
package com.cmc.dao.hibernate;

import java.util.List;

import com.cmc.dao.hibernate.BaseDaoHibernate;
import com.cmc.model.SystemRight;
import com.cmc.dao.SystemRightDao;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.HibernateCallback;

public class SystemRightDaoHibernate extends BaseDaoHibernate implements SystemRightDao {

    /**
     * @see com.cmc.dao.SystemRightDao#getSystemRights(com.cmc.model.SystemRight)
     */
    public List getSystemRights(final SystemRight systemRight) {
        //return getHibernateTemplate().find("from SystemRight");

        /* Remove the line above and uncomment this code block if you want 
           to use Hibernate's Query by Example API.*/
        if (systemRight == null) {
            return getHibernateTemplate().find("from SystemRight");
        } else {
            // filter on properties set in the systemRight
            HibernateCallback callback = new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException {
                    Example ex = Example.create(systemRight).ignoreCase().enableLike(MatchMode.ANYWHERE);
                    return session.createCriteria(SystemRight.class).add(ex).list();
                }
            };
            return (List)getHibernateTemplate().execute(callback);
        }
    }

    /**
     * @see com.cmc.dao.SystemRightDao#getSystemRight(Long id)
     */
    public SystemRight getSystemRight(final Long id) {
        SystemRight systemRight = (SystemRight) getHibernateTemplate().get(SystemRight.class, id);
        if (systemRight == null) {
            log.warn("uh oh, systemRight with id '" + id + "' not found...");
            throw new ObjectRetrievalFailureException(SystemRight.class, id);
        }

        return systemRight;
    }

    /**
     * @see com.cmc.dao.SystemRightDao#saveSystemRight(SystemRight systemRight)
     */    
    public void saveSystemRight(final SystemRight systemRight) {
        getHibernateTemplate().saveOrUpdate(systemRight);
    }

    /**
     * @see com.cmc.dao.SystemRightDao#removeSystemRight(Long id)
     */
    public void removeSystemRight(final Long id) {
        getHibernateTemplate().delete(getSystemRight(id));
    }
}
